/////////////////////////////////////////////////////////////////////////////
// Name:        addonswizard.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Sun Jul 22 16:16:30 2007
// RCS-ID:      
// Copyright:   (C) Victsoft 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"
#include "addonswizard.h"
#include "thememgr.h"
#include "cfgmgr.h"


InstallAddonsWizard::InstallAddonsWizard(wxWindow* parent, wxWindowID id, const wxPoint& pos )
{
    ////@begin InstallAddonsWizard creation
    SetExtraStyle(wxWIZARD_EX_HELPBUTTON);
    wxBitmap& wizardBitmap = ThemeManager::get().get_bitmap(wxT("wizard.png"));
    wxWizard::Create(parent, id, _("InstallAddonsWizard"), wizardBitmap, pos, wxDEFAULT_DIALOG_STYLE|wxCAPTION|wxSYSTEM_MENU|wxCLOSE_BOX|wxRESIZE_BORDER);

    CreateControls();
    Centre();
    InitEvents();

    //wxWizardPageSimple::Chain(p_failed_wizardpg, p_progress_wizardpg);
    wxWizardPageSimple::Chain(p_location_wizardpg, p_progress_wizardpg);
    wxWizardPageSimple::Chain(p_progress_wizardpg, p_installed_wizardpg);
    wxWizardPageSimple::Chain(p_installed_wizardpg, p_failed_wizardpg);
}


/*!
 * InstallAddonsWizard destructor
 */

InstallAddonsWizard::~InstallAddonsWizard()
{
////@begin InstallAddonsWizard destruction
////@end InstallAddonsWizard destruction
}


/*!
 * Control creation for InstallAddonsWizard
 */

void InstallAddonsWizard::CreateControls()
{
    p_location_wizardpg = new wxWizardPageSimple(this);
    GetPageAreaSizer()->Add(p_location_wizardpg);

    p_progress_wizardpg = new wxWizardPageSimple(this);
    GetPageAreaSizer()->Add(p_progress_wizardpg);

    p_installed_wizardpg = new wxWizardPageSimple(this);
    GetPageAreaSizer()->Add(p_installed_wizardpg);

    p_failed_wizardpg = new wxWizardPageSimple(this);
    GetPageAreaSizer()->Add(p_failed_wizardpg);
    
    ////@end InstallAddonsWizard content construction

    ////@begin LocationWizardPage content construction
    wxBoxSizer* p_location_sizer = new wxBoxSizer(wxVERTICAL);
    p_location_wizardpg->SetSizer(p_location_sizer);

    wxHtmlWindow* p_location_htmlwnd = new wxHtmlWindow(p_location_wizardpg, wxID_ANY, wxDefaultPosition, wxSize(399, 80), wxHW_SCROLLBAR_NEVER );
    p_location_htmlwnd->SetPage(_("<table>\n<tr>\n\t<td colspan=\"2\"><b>Install add-on</b></td>\n</tr>\n<tr>\n\t<td> </td>\n\t<td>Specify the location of the add-on, and then press <b>Next Button</b> to install.</td>\n</tr>\n</table>"));
    p_location_sizer->Add(p_location_htmlwnd, 0, wxGROW|wxBOTTOM, 5);

    wxBoxSizer* p_location_zip_sizer = new wxBoxSizer(wxHORIZONTAL);
    p_location_sizer->Add(p_location_zip_sizer, 1, wxGROW|wxBOTTOM|wxALIGN_CENTER_VERTICAL, 5);

    wxStaticText* p_location_zip_stc = new wxStaticText(p_location_wizardpg, wxID_STATIC, _("Add-on location:"));
    p_location_zip_sizer->Add(p_location_zip_stc, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_addon_file_text = new wxTextCtrl(p_location_wizardpg, ID_ADDON_ZIP_TEXT, _T(""));
    p_location_zip_sizer->Add(p_addon_file_text, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_browse_btn = new wxButton(p_location_wizardpg, ID_BROWSE_BTN, _("B&rowse..."));
    p_location_zip_sizer->Add(p_browse_btn, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_file_exist_stc = new wxStaticText(p_location_wizardpg, wxID_STATIC, _(""));
    p_file_exist_stc->SetForegroundColour(*wxRED);
    p_location_sizer->Add(p_file_exist_stc, 0, wxALL, 5);
    ////@end LocationWizardPage content construction

    ////@begin ProgressWizardPage content construction
    wxBoxSizer* p_progress_sizer = new wxBoxSizer(wxVERTICAL);
    p_progress_wizardpg->SetSizer(p_progress_sizer);

    wxHtmlWindow* p_progress_htmlwnd = new wxHtmlWindow(p_progress_wizardpg, wxID_ANY, wxDefaultPosition, wxSize(-1, 80), wxHW_SCROLLBAR_NEVER );
    p_progress_htmlwnd->SetPage(_("<table>\n<tr>\n\t<td colspan=\"2\"><b>Installing ...</b></td>\n</tr>\n<tr>\n\t<td> </td>\n\t<td>Add-on is being installed.</td>\n</tr>\n</table>"));
    p_progress_sizer->Add(p_progress_htmlwnd, 0, wxGROW|wxBOTTOM, 5);

    wxBoxSizer* p_installing_sizer = new wxBoxSizer(wxHORIZONTAL);
    p_progress_sizer->Add(p_installing_sizer, 1, wxGROW|wxALL, 5);
    wxBoxSizer* p_installing_v_sizer = new wxBoxSizer(wxVERTICAL);
    p_installing_sizer->Add(p_installing_v_sizer, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    
    p_installing_gauge = new wxGauge(p_progress_wizardpg, ID_INSTALLING_GAUGE, 100, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL|wxGA_SMOOTH );
    p_installing_gauge->SetValue(0);
    p_installing_v_sizer->Add(p_installing_gauge, 0, wxGROW|wxBOTTOM, 5);

    p_installing_stc = new wxStaticText(p_progress_wizardpg, wxID_STATIC, _("Installing..."), wxDefaultPosition, wxDefaultSize, 0 );
    p_installing_v_sizer->Add(p_installing_stc, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    ////@end ProgressWizardPage content construction
    
    ////@begin InstalledWizardPage content construction
    wxBoxSizer* p_installed_sizer = new wxBoxSizer(wxVERTICAL);
    p_installed_wizardpg->SetSizer(p_installed_sizer);

    wxHtmlWindow* p_installed_htmlwnd = new wxHtmlWindow(p_installed_wizardpg, wxID_ANY, wxDefaultPosition, wxSize(-1, 80), wxHW_SCROLLBAR_NEVER );
    p_installed_htmlwnd->SetPage(_("<table>\n<tr>\n\t<td colspan=\"2\"><b>Installed successfully</b></td>\n</tr>\n<tr>\n\t<td> </td>\n\t<td>You will need to <b>restart Daodict</b> for the changes to take effect.</td>\n</tr>\n</table>"));
    p_installed_sizer->Add(p_installed_htmlwnd, 0, wxGROW|wxBOTTOM, 5);

    wxBoxSizer* p_installed_h_sizer = new wxBoxSizer(wxHORIZONTAL);
    p_installed_sizer->Add(p_installed_h_sizer, 1, wxGROW|wxALL, 5);
    wxStaticBox* p_installed_nextstep_sizer_stc = new wxStaticBox(p_installed_wizardpg, wxID_ANY, _("Next step"));
    wxStaticBoxSizer* p_installed_nextstep_sizer = new wxStaticBoxSizer(p_installed_nextstep_sizer_stc, wxVERTICAL);
    p_installed_h_sizer->Add(p_installed_nextstep_sizer, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_exit_wizard_rbtn = new wxRadioButton(p_installed_wizardpg, ID_EXIT_WIZARD_RBTN, _("Exit this wizard"), wxDefaultPosition, wxDefaultSize, 0 );
    p_exit_wizard_rbtn->SetValue(true);
    p_installed_nextstep_sizer->Add(p_exit_wizard_rbtn, 0, wxGROW|wxALL, 5);

    p_restart_daodict_rbtn = new wxRadioButton(p_installed_wizardpg, ID_RESTART_DAODICT_RBTN, _("Restart Daodict now"), wxDefaultPosition, wxDefaultSize, 0 );
    p_restart_daodict_rbtn->SetValue(false);
    p_installed_nextstep_sizer->Add(p_restart_daodict_rbtn, 0, wxGROW|wxALL, 5);

    p_install_more_rbtn = new wxRadioButton(p_installed_wizardpg, ID_INSTALL_MORE_RBTN, _("Install more add-ons"), wxDefaultPosition, wxDefaultSize, 0 );
    p_install_more_rbtn->SetValue(false);
    p_installed_nextstep_sizer->Add(p_install_more_rbtn, 0, wxGROW|wxALL, 5);

    ////@end InstalledWizardPage content construction
    
    ////@begin FailedWizardPage content construction
    wxBoxSizer* p_failed_sizer = new wxBoxSizer(wxVERTICAL);
    p_failed_wizardpg->SetSizer(p_failed_sizer);

    wxHtmlWindow* p_failed_htmlwnd = new wxHtmlWindow(p_failed_wizardpg, wxID_ANY, wxDefaultPosition, wxSize(-1, 80), wxHW_SCROLLBAR_NEVER );
    p_failed_htmlwnd->SetPage(_("<table>\n<tr>\n\t<td colspan=\"2\"><b>Install failed</b></td>\n</tr>\n<tr>\n\t<td> </td>\n\t<td>You can press <b>Back Button</b> to reinstall the add-on.</td>\n</tr>\n</table>"));
    p_failed_sizer->Add(p_failed_htmlwnd, 0, wxGROW|wxBOTTOM, 5);

    wxBoxSizer* p_failed_h_sizer = new wxBoxSizer(wxHORIZONTAL);
    p_failed_sizer->Add(p_failed_h_sizer, 1, wxGROW|wxALL, 5);
    wxStaticBox* p_failed_nextstep_stc = new wxStaticBox(p_failed_wizardpg, wxID_ANY, _("Next step"));
    wxStaticBoxSizer* p_failed_nextstep_sizer = new wxStaticBoxSizer(p_failed_nextstep_stc, wxVERTICAL);
    p_failed_h_sizer->Add(p_failed_nextstep_sizer, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_f_exit_wizard_rbtn = new wxRadioButton(p_failed_wizardpg, ID_F_EXIT_WIZARD_RBTN, _("Exit this wizard"), wxDefaultPosition, wxDefaultSize, 0 );
    p_f_exit_wizard_rbtn->SetValue(false);
    p_failed_nextstep_sizer->Add(p_f_exit_wizard_rbtn, 0, wxGROW|wxALL, 5);

    p_f_install_more_rbtn = new wxRadioButton(p_failed_wizardpg, ID_F_INSTALL_MORE_RBTN, _("Install more add-ons"), wxDefaultPosition, wxDefaultSize, 0 );
    p_f_install_more_rbtn->SetValue(false);
    p_failed_nextstep_sizer->Add(p_f_install_more_rbtn, 0, wxGROW|wxALL, 5);

	////@end FailedWizardPage content construction
    
    // I don't know why, but if do not code following line, 
    // htmlwindow will not show text wrapped.
    p_location_wizardpg->SetMinSize(wxSize(380,100));
}


void InstallAddonsWizard::InitEvents() {
	this->Connect(ID_BROWSE_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&InstallAddonsWizard::OnBrowseAddon);
	
	this->Connect(ID_INSTALL_ADDONS_WIZARD, wxEVT_WIZARD_PAGE_CHANGING, 
			(wxObjectEventFunction)&InstallAddonsWizard::OnPageChanging);
	
	this->Connect(ID_INSTALL_ADDONS_WIZARD, wxEVT_WIZARD_PAGE_CHANGED, 
			(wxObjectEventFunction)&InstallAddonsWizard::OnPageChanged);
	
}


void InstallAddonsWizard::OnBrowseAddon(wxCommandEvent& event) {
	ConfigManager& cfgmgr = ConfigManager::get();
	wxString prev_dir = cfgmgr.Read(_T("/Addons/PrevOpenDir"), _T(""));
	
	wxFileDialog filedlg(this, _("Select Addon or Zip File"), prev_dir, _T(""), \
			_("Add-on (*.dict.zip; *.ext.zip; *.theme.zip)|*.dict.zip;*.ext.zip;*.theme.zip|Dictionary (*.dict.zip)|*.dict.zip|Extension (*.ext.zip)|*.ext.zip|Theme (*.theme.zip)|*.theme.zip"), \
			wxFD_OPEN|wxFD_FILE_MUST_EXIST);
	
	if (wxID_OK==filedlg.ShowModal()) {
		wxString filename = filedlg.GetPath();
		p_addon_file_text->SetValue(filename);
		
		prev_dir = wxFileName(filename).GetPath();
		cfgmgr.Write(_T("/Addons/PrevOpenDir"), prev_dir);
	}
}


void InstallAddonsWizard::OnPageChanging(wxWizardEvent& event) {
	if (p_location_wizardpg == event.GetPage()) {
		if (wxFileName::FileExists(p_addon_file_text->GetValue())) {
			p_file_exist_stc->SetLabel(_(""));
		} else {
			p_file_exist_stc->SetLabel(_("Error: The add-on path is invalid!"));
			event.Veto();
		}
		
		return;
	}
		
	//this->disable_next_btn();
}


void InstallAddonsWizard::OnPageChanged(wxWizardEvent& event) {	
	if (p_progress_wizardpg == event.GetPage()) {
		disable_next_btn();
		disable_prev_btn();
		wxMessageBox(_("intalling"));
		//ShowPage(p_progress_wizardpg->GetNext(), true);
	}
	
	//this->disable_next_btn();
}


void* InstallAddonsWizard::_install_addon_taskfunc(void* data) {
	
}


bool InstallAddonsWizard::run() {
	return RunWizard(p_location_wizardpg);
}
